<template>
    <div class="topbar">
        <div class="wrap topbarwrap">
            <div>欢迎来到叩丁狼积分商城</div>
            <ul class="loginul">
                <li>
                    <img :src="userInfo.headImg" alt="" />
                    <span>用户名：{{ userInfo.nickName }}</span>
                </li>
                <li>我的鸡腿：{{ userInfo.coin }}</li>
                <li>获取鸡腿</li>
                <li>叩丁狼官网</li>
                <li class="cart-btn" v-if="isLogined">
                    <img src="../assets/img/cart.png" alt="" />
                    <span>购物车</span>
                    <strong>{{ cartTotal }}</strong>
                </li>
                <li class="current" @click="changeIsShowLogin(true)" v-else>登录</li>
            </ul>
        </div>
    </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from 'vuex';
import { weixinLogin } from '@/request/api';

export default {
    data() {
        return {};
    },
    computed: {
        ...mapState({
            isLogined: state => state.loginstatus.isLogined,
            cartTotal: state => state.userInfo.cartTotal,
            userInfo: state => state.userInfo.userInfo,
        }),
    },
    created() {
        this.$nextTick(async () => {
            let mycode = this.$route.query.code;
            if (!mycode) {
                let mytoken = localStorage.getItem('x-auth-token');
                if (mytoken) {
                    this.asyncUpdateUserInfo();
                } else {
                    this.initUserInfo();
                }
                return;
            }
            let res = await weixinLogin({
                code: mycode,
            });
            if (res.code == 0) {
                alert('登陆成功');
                localStorage.setItem('x-auth-token', res['x-auth-token']);
                this.changeisLogined(true);
                this.asyncUpdateUserInfo();
            } else if (res.code == 400) {
                alert('请重新扫码登录');
                this.changeIsShowLogin(true);
            } else if (res.code == 407) {
                alert('请使用手机号再登录一次');
                this.changeIsShowLogin(true);
                localStorage.setItem('uuid', res.uuid);
            }
            this.$router.push(this.$route.path);
        });
    },
    methods: {
        ...mapMutations({
            changeIsShowLogin: 'showlogin/changeIsShowLogin',
            changeisLogined: 'loginstatus/changeisLogined',
            initUserInfo: 'userInfo/initUserInfo',
        }),
        ...mapActions({
            asyncUpdateUserInfo: 'userInfo/asyncUpdateUserInfo',
        }),
    },
};
</script>

<style lang="less" scoped>
.topbar {
    // width: 1920px;
    height: 40px;

    background: #242b39;
    .topbarwrap {
        display: flex;
        color: #fff;
        justify-content: space-between;
        line-height: 40px;
        .loginul {
            display: flex;

            li {
                margin-left: 20px;
                text-align: center;
                cursor: pointer;
                &.current {
                    background: #0a328e;
                    width: 124px;
                }

                img {
                    width: 24px;
                    height: 24px;
                    border-radius: 50%;
                }
                &.cart-btn {
                    width: 124px;
                    height: 40px;
                    background: #0a328e;
                    text-align: center;
                    img {
                        width: 20px;
                        height: 19px;
                        vertical-align: middle;
                    }
                    span {
                        margin: 0 8px;
                    }
                    strong {
                        width: 22px;
                        height: 22px;
                        line-height: 22px;
                        display: inline-block;
                        border-radius: 50%;
                        background-color: #fd604d;
                    }
                }
            }
        }
    }
}
</style>
